/*初始化datatable表格*/
function dataTableInit(url, columns) {
    $("#datatable").DataTable({
        "scrollY": "500px",//设置表格出现滚动条的高度
        "pageLength": 10,//设置默认的每页行数，默认为10
        "processing": true,//设置是否显示正在处理的状态,默认为false
        "lengthMenu": [10, 15, 20, 50, 100],//设置可选的每页显示行数列表,默认为[10,25,50,100]
        "scrollCollapse": true,//当数据少于规定每页行数是是否改变表格高度,默认为false
        "pagingType": "full_numbers",//分页按钮显示选项,默认为"simple_numbers"
        "language": {//信息汉化
            "lengthMenu": "每页 _MENU_ 条记录",
            "zeroRecords": "无匹配结果",
            "info": "当前 _START_ 条到 _END_ 条 共 _TOTAL_ 条",
            "infoEmpty": "无记录",
            "infoFiltered": "(从 _MAX_ 条记录过滤)",
            "search": "搜索",
            "processing": "载入中",
            "paginate": {
                "first": "首页",
                "previous": "上一页",
                "next": "下一页",
                "last": "尾页"
            }
        },
        columns: columns,//定义列
        dom: "<'row'<'col-sm-2'l><'col-sm-4 pull-left'f><'col-sm-6 text-right'B>>rtip",//设置组件dom顺序，增加样式ID
        buttons: {//增加按钮
            buttons: [{extend: 'csv', text: 'csv', className: 'btn btn-info'},
                {extend: 'excel', text: 'excel', className: 'btn btn-danger'},
                {text:"刷新",className: 'btn btn-success',action: function () {
                        this.ajax.reload();}}]
        },
        ajax: {
            "url": url,
            "dataSrc": function (json) {
                if (json.code === 200) {
                    return json.msg;
                }
            }
        }
    });
}

//addAjax请求
function addAjax(data, ajaxUrl, contentType, processData, addModalId) {
    $.ajax({
        type: 'POST',
        url: ajaxUrl,
        cache: false,
        contentType: contentType,
        processData: processData,
        async: false,
        dataType: "json",
        data: data,
        success: function (result) {
            if (result.code === 200) {
                $(addModalId).modal('hide');
                $('#datatable').DataTable().ajax.reload();
            } else {
                console.log(result.msg);
            }
        },
        error: function (result) {
            return result;
        }
    });
    return false;
}

//updateAjax请求
function updateAjax(data, ajaxUrl, contentType, processData, updateModalId) {
    $.ajax({
        type: 'POST',
        url: ajaxUrl,
        cache: false,
        async: false,
        contentType: contentType,
        processData: processData,
        dataType: "json",
        data: data,
        success: function (result) {
            if (result.code === 200) {
                $(updateModalId).modal('hide');
                $('#datatable').DataTable().ajax.reload();
            } else {
                alert(result.msg);
            }
        },
        error: function (result) {
            return result;
        }
    });
    return false;
}

//为delete按钮绑定delete事件
function deleteBind(ajaxUrl) {
    $("#datatable").on("click", ".data-delete", function () {
        let row = $("#datatable").DataTable().row($(this).parents('tr'));
        let _id = $(this).attr("data-id");
        if (confirm("确定删除？")) {
            $.ajax({
                url: ajaxUrl,
                type: 'GET',
                async: true,
                data: {id: _id},
                contentType: 'application/json;charset=UTF-8',
                cache: false,
                error: function (result) {
                    return result.msg;
                },
                success: function (result) {
                    if (result.code === 200) {
                        row.remove().draw(false);
                    }
                }
            });
        }
    });
}

//为add按钮绑定add事件
function addBind(addFormId, ajaxUrl, contentType, processData, addModalId) {
    $(addFormId).parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($(addFormId)[0]);
        addAjax(data, ajaxUrl, contentType, processData, addModalId);
        return false;
    });
}

//为update按钮绑定update事件
function updateBind(updateFormId, ajaxUrl, contentType, processData, updateModalId) {
    $("#datatable").on('click', ".data-update", function () {
        //点击更新按钮获取此行数据的id用于后面的ajax更新
        let data_id = $(this).attr('data-id');
        //获取此行数据
        let row = $("#datatable").DataTable().row($(this).parents('tr')).data();
        //遍历更新表单的所有拥有name的输入框，将行数据填充到这些输入框中，file除外
        $(updateFormId+" [name]").each(function () {
            if($(this).attr('type')!=="file"){
                let name =  $(this).attr("name");
                $(this).val(row[name]);
            }
        });
        updateSubmit(data_id, updateFormId, ajaxUrl, contentType, processData, updateModalId);
    });
}

function updateSubmit(data_id, updateFormId, ajaxUrl, contentType, processData, updateModalId) {
    $(updateFormId).parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let id = data_id;
        let data = new FormData($(updateFormId)[0]);
        data.set("id", id);
        updateAjax(data, ajaxUrl, contentType, processData, updateModalId);
        return false;
    });
}

